/*
 * ChatListenManager.java
 *
 * Created on 2007年5月15日, 上午11:01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ecomm.chat.manager;
import com.ecomm.chat.pojo.ChatGroup;
import com.ecomm.chat.pojo.ChatListen;
import com.ecomm.platcore.service.BaseEntityManager;
import com.ecomm.util.DB;
import com.ecomm.util.DBFactory;
import com.ecomm.util.HibernateUtil;
import com.ecomm.util.SpringUtil;
import com.ecomm.util.UUID;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.hibernate.Session;

/**
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>pijunliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @since 2007年5月15日, 上午11:01
 */
public class ChatListenManager extends BaseEntityManager<ChatListen> {
  
  public  ChatListenManager() {
  }
  private static ChatListenManager chatListenManager = null;
  
  public static ChatListenManager getInstance(){
    if(chatListenManager == null){
      chatListenManager = (ChatListenManager)SpringUtil.getBean("chatListenManager");
    }
    return chatListenManager;
  }
  /**
   *加入到当前的某个聊天实例中
   *
   */
  public void jionPushlet(String id,String userid){
    if(id.indexOf("chat") >= 0){
      //单聊
      ChatListen chatlis = (ChatListen) getHibernateTemplate().get(ChatListen.class,id);
      if(chatlis.getJoinpeople() != null &&  chatlis.getJoinpeople().indexOf(userid) <=0){
        chatlis.setJoinpeople(userid+","+chatlis.getJoinpeople());
        this.getHibernateTemplate().saveOrUpdate(chatlis);
      }else{
        chatlis.setJoinpeople(userid+",");
        this.getHibernateTemplate().saveOrUpdate(chatlis);
      }
    }else{
      //群聊
      ChatGroup chatg = (ChatGroup) getHibernateTemplate().get(ChatGroup.class,id);
      if(chatg != null && chatg.getJoinpeople() != null && chatg.getJoinpeople().indexOf(userid) <=0){
        chatg.setJoinpeople(userid+","+chatg.getJoinpeople());
        this.getHibernateTemplate().saveOrUpdate(chatg);
      }else{
        chatg.setJoinpeople(userid+",");
        this.getHibernateTemplate().saveOrUpdate(chatg);
      }
    }
  }
  /**
   *退出 某个聊天实例
   */
  public void LeavePushlet(String id,String userid){
    if(id.indexOf("chat") >= 0){
      //单聊
      ChatListen chatlis = (ChatListen) getHibernateTemplate().get(ChatListen.class,id);
      if(chatlis.getJoinpeople() != null && chatlis.getJoinpeople().indexOf(userid) >=0){
        chatlis.setJoinpeople(chatlis.getJoinpeople().replaceAll(userid+",",""));
        this.getHibernateTemplate().saveOrUpdate(chatlis);
      }
    }else{
      //群聊
      ChatGroup chatg = (ChatGroup) getHibernateTemplate().get(ChatGroup.class,id);
      if(chatg != null && chatg.getJoinpeople() != null &&  chatg.getJoinpeople().indexOf(userid) >=0){
        chatg.setJoinpeople(chatg.getJoinpeople().replaceAll(userid+",",""));
        this.getHibernateTemplate().saveOrUpdate(chatg);
      }
    }
  }
  /**
   * public String chatListen(String sendUseId,String acceptUserId){
   * if(sendUseId == null || sendUseId.equals("")
   * || acceptUserId == null || acceptUserId.equals("")){
   * return "";
   * }
   * String  hql = "SELECT id FROM ChatListen WHERE " +
   * "(acceptuserid = '"+sendUseId+"') AND (senduserid = '"+acceptUserId+"') " +
   * "OR (acceptuserid = '"+acceptUserId+"') AND (senduserid = '"+sendUseId+"')";
   * String listen = "";
   * listen = (String)this.find(hql);
   * if(listen == null || listen.equals("")){
   * listen = UUID.create("chat");
   * ChatListen cl = new ChatListen();
   * cl.setId(listen);
   * cl.setAcceptuserid(acceptUserId);
   * cl.setSenduserid(sendUseId);
   * this.getHibernateTemplate().save(cl);
   * }
   * return listen;
   * }
   */
  /**
   *根据 发送人 和接收人 去查找监听ID
   **/
  public String chatListen(String sendUseId,String acceptUserId){
    if(sendUseId == null || sendUseId.equals("")
    || acceptUserId == null || acceptUserId.equals("")){
      return "";
    }
    String listen = "";
    String  sSQL = "SELECT id FROM chat_listen WHERE " +
        "(acceptuserid = '"+sendUseId+"') AND (senduserid = '"+acceptUserId+"') " +
        "OR (acceptuserid = '"+acceptUserId+"') AND (senduserid = '"+sendUseId+"')";
    DB db=null;
    String name ="" ;
    try {
      if(db ==null)
        db= DBFactory.getDBInstance();
      PreparedStatement pst = null;
      pst = db.createPreparedStatement(sSQL);
      ResultSet rs = pst.executeQuery();
      if (rs.next()) {
        listen =  (String)rs.getString("id");
      }else{
        
        listen = UUID.create("chat");
        sSQL ="insert into chat_listen(ID,SENDUSERID,ACCEPTUSERID) values('"+listen+"','"+sendUseId+"','"+acceptUserId+"')";
        int k_ch =  db.executeUpdate(sSQL);
        if(k_ch < 0){
          System.out.println("cuowu ---------"+k_ch+":"+listen);
          listen = "chat";
        }
      }
      if (rs!=null) {
        rs.close();
        rs = null;
      }
      pst.close();
      pst = null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if(db!=null)
        db.close();
      return listen;
    }
  }
  /**
   *根据ID得到监听的对象
   */
  public ChatListen getChatliseten(String id){
    if(id ==  null || id.equals("")){
      return null;
    }
    return  (ChatListen)getHibernateTemplate().get(ChatListen.class,id);
  }
  /**
   *根据ID删除加入人的名字
   */
  public void deleteListenJionName(String id){
    ChatListen cl = this.getChatliseten(id);
    if(cl != null && cl.getJoinpeoplename() != null){
      cl.setJoinpeoplename(""); //直接为空，是在系统加载的时候
      this.getHibernateTemplate().update(cl);
    }
  }
  /**
   *更改加入人的名字，是有多少人追加近来了
   */
  public void updateListenJionName(String id,String userids){
    if(id !=  null && !id.equals("")
    && userids !=  null && !userids.equals("")){
      ChatListen cl = (ChatListen)getHibernateTemplate().get(ChatListen.class,id);
      
      if(userids.indexOf(cl.getAcceptuserid()) >= 0){//包括
        userids = userids.replaceAll(cl.getAcceptuserid()+",","");
      }
      if(userids.indexOf(cl.getSenduserid()) >= 0){//包括
        userids = userids.replaceAll(cl.getSenduserid()+",","");
      }
      // System.out.println("userids -------"+userids);
      cl.setJoinpeoplename(userids);
      this.getHibernateTemplate().update(cl);
    }
  }
  public static void main(String[] args) {
    String s = "23,";
    System.out.println(s.replaceAll("23,",""));
    
  }
}
